package net.dl.ss.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.dl.ss.dao.AdjudicatorDao;
import net.dl.ss.domain.Adjudicator;
import net.dl.ss.service.ContextManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author yakovlev.yuriy@gmail.com
 * @since 12.8.2008
 */
@Controller
public class AdjudicatorsController extends BaseController {
	protected final Log logger = LogFactory.getLog(getClass());
	private final AdjudicatorDao adjudicatorDao;
	
	@Autowired
	public AdjudicatorsController(AdjudicatorDao adjudicatorDao) {
		this.adjudicatorDao = adjudicatorDao;
	}
	
	@RequestMapping("/adjudicatorsList.htm")
	protected void adjudicatorsListHandler(HttpServletRequest request, HttpServletResponse response) {
		try {
			ServletOutputStream  os = response.getOutputStream();
			modifyResponseHeader(response, "text/xml");
			
		    os.println("<adjudicators>");
		    Long eventId = ContextManager.getCurrentEvent(request);
		    if(eventId!=null) {
		    	List<Adjudicator> adjudicators = this.adjudicatorDao.findByEventId(eventId);
		    	for(Adjudicator adjudicator : adjudicators) {
		    		os.print(adjudicator.toXML());
		    	}
		    }	
		    os.println("</adjudicators>");
	
			os.close();
		} catch (IOException e) {
			logger.error(e.getMessage());
		} 
	}
	
	@RequestMapping("/categAdjudicatorsList.htm")
	protected void categAdjudicatorsListHandler(HttpServletRequest request, HttpServletResponse response) {
		try {
			ServletOutputStream  os = response.getOutputStream();
			modifyResponseHeader(response, "text/xml");
			
		    os.println("<adjudicators>");
		    Long categoryId = ContextManager.getCurrentCategory(request);
		    if(categoryId!=null) {
		    	List<Adjudicator> adjudicators = this.adjudicatorDao.findByCategoryId(categoryId);
		    	for(Adjudicator adjudicator : adjudicators) {
		    		os.print(adjudicator.toXML());
		    	}
		    }	
		    os.println("</adjudicators>");
	
			os.close();
		} catch (IOException e) {
			logger.error(e.getMessage());
		} 
	}
	
	@RequestMapping("/adjudicatorUpdate.htm")
	public void adjudicatorUpdateHandler(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletOutputStream  os = response.getOutputStream();
		modifyResponseHeader(response, "text/plain");

		Long eventId = ContextManager.getCurrentEvent(request);
	    if(eventId!=null) {
	    	Adjudicator adjudicator = new Adjudicator();
			adjudicator.setEventId(eventId);
			adjudicator.setName(request.getParameter("name"));
			adjudicator.setLetter(request.getParameter("letter"));
			try {
				String id = request.getParameter("id");
				if(id!=null && !"".equals(id)) {
					adjudicator.setId(Long.parseLong(id));
					this.adjudicatorDao.updateEntity(adjudicator);
				} else {
					this.adjudicatorDao.saveEntity(adjudicator);
				}
				os.print("{success:true}");
			} catch(Exception ex) {
				logger.error(ex.getMessage());
			}
	    } else {
	    	os.print("{success:false}");
	    }
	    os.close();
	}
	
	@RequestMapping("/adjudicatorDelete.htm")
	public void adjudicatorDeleteHandler(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletOutputStream  os = response.getOutputStream();
		modifyResponseHeader(response, "text/plain");
		try {
			String id = request.getParameter("id");
			if(id!=null && !"".equals(id)) {
				this.adjudicatorDao.deleteEntityById(Long.parseLong(id));
				os.print("{success:true}");
			} else {
				os.print("{success:false}");
			}
		} catch(Exception ex) {
			logger.error(ex.getMessage());
		}
	    os.close();
	}

}
